<template>
	<view class="channel">
		<swiper
			:indicator-dots="swiper.indicatorDots"
			:autoplay="swiper.autoplay"
			:interval="swiper.interval"
			:duration="swiper.duration"
			:circular="swiper.circular"
			style="width:100%;height:320rpx;flex:1;"
		>
			<swiper-item v-for="(swipe, index) in swiper.data">
				<navigator :url="swipe.src"><image :src="swipe.url" :lazy-load="true" mode="aspectFill" style="width:100%;height:320rpx;flex:1;"></image></navigator>
			</swiper-item>
		</swiper>
		<custom-news-list :item="channelData"></custom-news-list>
		<custom-gotop v-show="isShowGoTop"></custom-gotop>
	</view>
</template>

<script>
export default {
	data() {
		return {
			isShowGoTop: false,
			swiper: {
				indicatorDots: true,
				autoplay: true,
				interval: 2000,
				duration: 500,
				circular: true,
				data: []
			},
			channelData: {
				data: []
			},
			channelId: '',
			loading: false,
			loaded: false,
			pn: 0
		};
	},
	onShow() {
		this.boolLogin();
	},
	onLoad(ops) {
		this.channelId = ops.id;
		this.boolLogin(this.getPageData);
	},
	onPullDownRefresh() {
		this.boolLogin(this.getPageData);
	},
	onReachBottom() {
		let that = this;
		this.boolLogin().then(() => {
			if (!that.loading && !that.loaded) {
				that.loading = true;
				uni.showLoading({
					title: '正在拉取中',
					mask: true,
					complete() {
						that.getChannelListData({ pn: that.pn + 1 }).finally(() => {
							that.loading = false;
							that.pn += 1;
							uni.hideLoading();
						});
					}
				});
			}
		});
	},
	onPageScroll(v) {
		if (v.scrollTop >= 200) {
			this.isShowGoTop = true;
		} else {
			this.isShowGoTop = false;
		}
	},
	methods: {
		boolLogin(callback) {
			let that = this;
			return new Promise((resolve, reject) => {
				that._plugins
					.boolLogin()
					.then(() => {
						callback && callback();
						resolve();
					})
					.catch(() => {
						uni.navigateTo({
							url: '../login/login'
						});
					});
			});
		},
		getPageData() {
			let that = this;
			uni.showLoading({
				title: '正在拉取中',
				mask: true,
				complete() {
					Promise.all([
						that.getAdData({
							page: 'index',
							type: 'swiper'
						}),
						that.getChannelListData({ pn: 0 })
					])
						.then(res => {
							console.log(res);
						})
						.catch(err => {
							console.log(err);
						})
						.finally(() => {
							uni.stopPullDownRefresh();
							uni.hideLoading();
						});
				}
			});
		},
		getAdData(json) {
			let that = this;
			return new Promise((resolve, reject) => {
				that.$store
					.dispatch('getAdData', json)
					.then(res => {
						console.log(res);
						that.swiper.data = res.data;
						resolve();
					})
					.catch(err => {
						console.log(err);
						reject(err);
					});
			});
		},
		getChannelListData(json) {
			let that = this;
			return new Promise((resolve, reject) => {
				that.$store
					.dispatch('getChannelListData', { ...json })
					.then(res => {
						console.log(res);
						that.channelData.data = json.pn > 0 ? that.channelData.data.concat(res.data) : res.data;
						resolve();
					})
					.catch(err => {
						console.log(err);
						reject(err);
					});
			});
		}
	}
};
</script>

<style lang="less">
.channel {
}
</style>
